<template>
<div>
  <div id="app">
    <el-card style="width: 500px;margin: auto">
      <div slot="header" class="clearfix">
        <span>个人资料</span>
      </div>
      <el-form label-width="60px" size="small">
        <el-form-item label="用户名">
          <el-input v-model="form.username" disabled autocomplete="off" ></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:8082/file/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="form.photo" :src="form.photo" style="width: 100px; height: 100px" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.sex" placeholder="请选择">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份">
          <el-input v-model="form.identity" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="擅长">
          <el-input v-model="form.specialty" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input type="textarea" v-model="form.outline" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="医院">
          <el-select clearable v-model="form.hoId" placeholder="请选择">
            <el-option v-for="item in hospitals" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科室">
          <el-select clearable v-model="form.deId" placeholder="请选择">
            <el-option v-for="item in departments" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</div>
</template>

<script>
export default {
  name: "PhyPerson",
  data() {
    return {
      form: {},
      user: {},
      hospitals: {},
      departments: {},
    }
  },
  created() {
    this.request.get("/hospital/list").then(res => {
      this.hospitals = res
    })
    this.request.get("/physician/findDep").then(res => {
      this.departments = res
    })
    this.load()
  },
  methods:{
    load(){
      let username = localStorage.getItem("phy") ? JSON.parse(localStorage.getItem("phy")).username : ""
      if (username) {
        this.request.post("/physician/findByName?username=" + username).then(res => {
          if (res) {
            res.token = this.user.token
            this.form = res
            console.log(this.form)
            localStorage.setItem("phy",JSON.stringify(res))
            //console.log(res.password)
          }
        })
      }
    },
    save() {
      this.request.post("/physician/editUser", this.form).then(res => {
        if (res) {
          this.$message.success("保存成功")
          this.$emit('refreshUser')
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAvatarSuccess(res) {
      this.form.photo = res
    }
  }
}
</script>

<style scoped>
#app{
  padding-top: 20px;
  display:flex;
  justify-content: center;
  align-items: center;
}
</style>

